package com.example.team_awesome_funf;


import edu.mit.media.funf.json.IJsonObject;
import edu.mit.media.funf.pipeline.BasicPipeline;

public class ProbeDataAnalyzer {
	
	DBAccess DB;
	
	public ProbeDataAnalyzer()
	{
		DB = new DBAccess();
	}
	
	// Parsing to JSON-data to a LocationProbeData object
	public LocationProbeData ParseLocationProbeData(IJsonObject data)
	{
		LocationProbeData probeData = new LocationProbeData();
		probeData.Speed = data.getAsJsonPrimitive("mSpeed").getAsFloat();
		probeData.Latitude = data.getAsJsonPrimitive("mLatitude").getAsFloat();
		probeData.Longitude =  data.getAsJsonPrimitive("mLongitude").getAsFloat();
		probeData.TimeStamp = data.getAsJsonPrimitive("timestamp").getAsFloat();
		return probeData;
	}
	
	// Guessing transport based on speed
	public String AnalyzeMovementSpeed(float speed)
	{
		String returnString = "";
		if(speed > 32.0) returnString += " (so you are problably driving)";
		else if(speed > 13.0) returnString += " (so you are problably biking)";
		else if(speed < 13 && speed > 7.5) returnString += " (so you are problably running)";
		else if(speed < 7.5 && speed > 1.0) returnString += " (so you are problably walking)";
		else returnString += " (so you are problably standing still)";
		
		return returnString;
	}
	
	// Retrieve all location probe data, that has not been archived yet
	public String GetMovementSpeedFromLocationProbe(BasicPipeline pipeline)
	{
		String locProbeData[];
		String returnString = "";
		double averageSpeed = 0;
		
		locProbeData = DB.GetLocationProbeData(pipeline.getDb());
		if(locProbeData != null && locProbeData.length > 0)
		{
			//For each registered location probe data since the last archiving
			for(int i = 0; i < locProbeData.length; i++ )
			{
				System.out.println(locProbeData[i]);
				//Speed in m/s from parsed probe data
				double speed = Double.parseDouble(locProbeData[i].substring(locProbeData[i].indexOf("mSpeed")+8, locProbeData[i].indexOf("mTime")-2));
				
				averageSpeed += speed;
				
				returnString = returnString + String.valueOf(speed);
				
				if(speed > 10.0) returnString += " (so you are problably driving) \n";
				else if(speed > 4.1) returnString += " (so you are problably biking) \n";
				else if(speed < 4.1 && speed > 2.0) returnString += " (so you are problably running) \n";
				else if(speed < 2.0 && speed > 0.5) returnString += " (so you are problably walking) \n";
				else returnString += " (so you are problably standing still) \n";
			}
			
			returnString += "Average speed: " + String.valueOf(averageSpeed/locProbeData.length) + " m/s";
			//String tempStr = DB.GetAccData(pipeline.getDb());
		}
		
		return returnString;
	}

}
